package com.pluginsacceptor.plugin.formula.statistics;

import java.util.Arrays;

import com.pluginsacceptor.plugin.formula.MathsTrigo.MathsTrigo;
import com.pluginsacceptor.plugin.formula.MathsTrigo.MathsTrigoAPI;


public class Statistics implements StatisticsAPI {

	@Override
	public double moyenne(double[] tab) {
		
		MathsTrigo s = new MathsTrigo();
		double somme = s.somme(tab);
		
		double n = tab.length;
		
		 return (somme/n);
			
	}

	@Override
	public double max(double[] tab) {
		double max = tab[0];
        for (int i = 1; i < tab.length; i++) {
            if (tab[i] > max) max = tab[i];
        }
        return max;
    }

	

	@Override
	public double min(double[] tab) {
		double min = tab[0];
        for (int i = 1; i < tab.length; i++) {
            if (tab[i] < min) min = tab[i];
        }
        return min;
	}


	@Override
	public double moyenneHarmo(double[] tab) {
		
		double moy = moyenne(tab);
		
		return (1/moy);
	}

	@Override
	public double ecartType(double[] tab) {
		return Math.sqrt(variance(tab));
	}

	@Override
	public double mediane(double[] tab) {
		
        Arrays.sort(tab);
		
        if (tab.length % 2 == 0) 
        {
           return (tab[(tab.length / 2) - 1] + tab[tab.length / 2]) / 2.0;
        } 
        else 
        {
           return tab[tab.length / 2];
        }		
		
	}

	
	@Override
	public double pourcentage(double a, double b) {
		double pourcentage = (a/b) * 100;
		return pourcentage;
	}

	@Override
	public double nb(double[] tab) {
		return tab.length;
	}

	@Override
	public double variance(double[] tab) {

		if (tab.length == 0) return Double.NaN;
        double moy = moyenne(tab);
        double somme = 0.0;
        for (int i = 0; i < tab.length; i++) {
            somme += ((tab[i] - moy) * tab[i] - moy);
        }
        return (somme / (tab.length - 1));
		
		
		}

}
